<template>
  <div style="margin-left: 5rem">
    <el-text>角色信息:</el-text>
    <el-button type="primary" size="small" style="float: right" @click="changeEvent=true">新增角色</el-button>
    <el-table
        :data="this.tableData"
        style="width: 45rem"
        height="250">
      <el-table-column
          prop="roleNo"
          label="角色编号"
          width="120">
      </el-table-column>
      <el-table-column
          fixed
          prop="roleName"
          label="角色名称"
          width="150">
      </el-table-column>
      <el-table-column
          prop="roleDescribe"
          label="角色描述"
          width="120">
      </el-table-column>
      <el-table-column prop="operate" label="操作" width="auto">
        <template v-slot="scope">
          <el-button type="primary" size="small" @click="editData(scope.row.id)">编辑</el-button>
          <el-button type="danger" size="small" @click="deleteData(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>

  <el-dialog title="操作角色信息" style="width: 30rem;" v-model="this.changeEvent" :close-on-click-modal="false"
             :show-close="false">
    <el-form ref="rowData" :model="this.rowData" label-width="80px" style="margin-left: 3rem">
      <el-form-item label="角色类型">
        <!--v-model双向绑定数据-->
        <!-- 1rem===16px-->
        <el-select v-model="this.rowData.roleNo">
          <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="角色名称">
        <el-input
            style="width: 15rem;"
            v-model="this.rowData.roleName"
        ></el-input>
      </el-form-item>
      <el-form-item label="角色描述">
        <el-input
            style="width: 15rem;"
            v-model="this.rowData.roleDescribe"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="this.submitRowData()">提交</el-button>
        <el-button @click="this.closeDialog()">取消</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>

</template>

<script>
import request from "@/utils/request.js";

export default {
  name: "Role",
  data() {
    return {
      // 弹出框
      changeEvent: false,
      // 表格所有数据
      tableData: [],
      // 编辑行数据
      rowData: {},
      // 角色静态对应数据
      options: [{
        value: '20231111',
        label: '管理员'
      }, {
        value: '20232222',
        label: '普通用户'
      }
      ],
    }
  },
  created() {
    // this.getTableData();
  },
  methods: {
    getTableData() {
      request.get("/role/all").then(res => {
        if (res.data.code === '200') {
          this.tableData = res.data.data;
        }
      })
    },
    //新增或者编辑航班数据
    editData(id) {
      // 获取行数据
      request.get("/role/" + id).then(res => {
        if (res.data.code === '200') {
          this.rowData = res.data.data;
        }
      })
      // 显示弹窗
      this.changeEvent = true;
    },
    // 删除航班数据
    deleteData(id) {
      request.get("/role/delete/" + id).then(res => {
        if (res.data.code === '200') {
          // 重新获取表格数据
          this.getTableData();
          // 清空行数据
          this.rowData = {};
          // 显示提交成功
          this.$message.success("操作成功")
        }
      })
    },
    // 提交行数据
    submitRowData() {
      request.post("/role/edit", this.rowData).then(res => {
        if (res.data.code === '200') {
          //关闭弹窗
          this.changeEvent = false;
          // 重新获取表格数据
          this.getTableData();
          // 清空提交信息
          this.rowData = {};
          // 显示提交成功
          this.$message.success("提交成功")
        }
      })
    },
    closeDialog() {
      // 关闭弹窗
      this.changeEvent = false;
      // 清空数据
      this.rowData = {};
    }
  }
}


</script>


<style>

</style>